package edu.xpu.project.record.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import edu.xpu.project.record.entity.BusBespeakRecord;
import edu.xpu.project.record.service.BusBespeakRecordService;
import edu.xpu.project.user.entity.ProjectUser;
import edu.xpu.project.vehicle.entity.BusSchedule;
import edu.xpu.project.vehicle.service.BusScheduleService;

/**
 * @ClassName: BusBespeakRecordController.java
 * @Description: 校车预约控制器
 *
 * @author liukang
 * @date 2017年5月9日 下午1:56:43
 * @version 
 * @since JDK 1.6
 */
@Controller
@RequestMapping(value="/busBespeak")
@SessionAttributes("sessionUser")
public class BusBespeakRecordController {
	
	@Autowired
	private BusBespeakRecordService bespeakRecordService;
	@Autowired
	private BusScheduleService busScheduleService;
	
	/**
	  * @MethodName: busBespeakDetails
	  * @Description: 预约记录详情
	  * @Author liukang
	  * @date 2017年5月23日  下午12:22:20
	 */
	@RequestMapping(value="busBespeakDetails/{id}")
	public String busBespeakDetails(@PathVariable(value="id") Long id, ModelMap modelMap) {
		BusBespeakRecord busBespeakRecord = bespeakRecordService.get(id);
		modelMap.addAttribute("busBespeakRecord", busBespeakRecord);
		return "record/busBespeak/busBespeakDetail";
	}
	
	/**
	 * @describe 预约记录删除
	 * @date 2017年5月23日
	 * @author liukang
	 */
	@ResponseBody
	@RequestMapping(value="busBespeakDel/{id}")
	public String busBespeakDel(@PathVariable(value="id") Long id) {
		try {
			bespeakRecordService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		return "{\"status\":\"success\"}";
	}
	
	/**
	  * @MethodName: saveBusBespeakRecord
	  * @Description: 保存学生信息
	  * @param bespeakRecord
	  * @param modelMap
	  * @return String    返回类型
	  * @throws
	  * 
	  * @Author liukang
	  * @date 2017年5月9日  下午2:09:10
	 */
	@ResponseBody
	@RequestMapping(value="saveBusBespeakRecord")
	public String saveBusBespeakRecord(BusBespeakRecord bespeakRecord, ModelMap modelMap, @ModelAttribute("sessionUser") ProjectUser projectUser, HttpSession session) {
		if (bespeakRecord.getId() == null) {
			bespeakRecord.setCreateDate(new Date());
			//ProjectUser projectUser2 = projectUserService.get(projectUser.getId());
			ProjectUser p = (ProjectUser) session.getAttribute("sessionUser");
			bespeakRecord.setProjectUser(p);
			
		}
		try {
			bespeakRecordService.saveOrUpdate(bespeakRecord);
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		return "1";
	}
	
	@RequestMapping(value="toBusBespeakPage")
	public String toBusBespeakPage() {
		
		return "record/busBespeak/busBespeakAdd";
	}
	
	/**
	 * @describe 到教师申请校车页面
	 * @date 2017年5月12日
	 * @author liukang
	 */
	@RequestMapping(value="/toTeacherApplyPage")
	public String toTeacherApplyPage(ModelMap modelMap, @RequestParam(value="scheduleId") Long scheduleId) {
		//根据预约车次获取列车时刻信息
		BusSchedule busSchedule = busScheduleService.get(scheduleId);
		modelMap.addAttribute("busSchedule", busSchedule);
		return "teacher/index/busScheduleApplyAdd";
	}
	
	/**
	  * @MethodName: busBespeakList
	  * @Description: 校车预约列表
	  * @param pageNumber
	  * @param pageSize
	  * @param bespeakRecord
	  * @param modelMap
	  * @return String    返回类型
	  * @throws
	  * 
	  * @Author liukang
	  * @date 2017年5月9日  下午2:01:22
	 */
	@RequestMapping(value="/busBespeakList")
	public String busBespeakList(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			BusBespeakRecord bespeakRecord, ModelMap modelMap) {
		Page<BusBespeakRecord> pageByCriterias = bespeakRecordService.getPageByCriterias(pageNumber, pageSize, bespeakRecord);
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("bespeakRecord", bespeakRecord)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize);
		return "record/busBespeak/busBespeakList";
	}
	
	@RequestMapping(value="/busBespeaTeacherkList/{teacherId}")
	public String busBespeaTeacherkList(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			BusBespeakRecord bespeakRecord, ModelMap modelMap,
			@PathVariable(value="teacherId") Long teacherId) {
		ProjectUser projectUser = new ProjectUser();
		projectUser.setId(teacherId);
		bespeakRecord.setProjectUser(projectUser);
		//查询所有
		Page<BusBespeakRecord> pageByCriterias = bespeakRecordService.getPageByCriterias(pageNumber, pageSize, bespeakRecord);
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("bespeakRecord", bespeakRecord)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize);
		return "teacher/index/busBespeakTeacherList";
	}

}

